/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductPnl.java
 *
 * Created on May 1, 2010, 10:14:11 PM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ProductBiz;
import com.aptech.campaign.component.ColumnData;
import com.aptech.campaign.component.ObjectTableModel;
import com.aptech.campaign.entities.Product;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

/**
 *
 * @author Tai Nguyen
 */
public class ProductPnl extends javax.swing.JPanel {

    CampaignMainFrm main;
    ProductBiz proBuz = new ProductBiz();
    public ObjectTableModel tableModel;
    public ArrayList<Product> arr = null;
    public JTable headerTable;

    public ProductPnl() {
        //     arr = new ArrayList<Product>();
        initComponents();
        loadTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * 
     */
    public void loadTable() {
        arr = proBuz.findByAllProduct();
        ColumnData[] columns = {
            new ColumnData("Name", 100, SwingConstants.LEFT, 2),
            new ColumnData("Price", 50, SwingConstants.LEFT, 3),
            new ColumnData("Info", 150, SwingConstants.LEFT, 4)
        };

        tableModel = new ObjectTableModel(tbProduct, columns, arr);
        headerTable = tableModel.getHeaderTable();

        //Auto set ordinal
        headerTable.createDefaultColumnsFromModel();
        tbProduct.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();

        //display data with ordinal
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scrProduct.setRowHeader(viewport);
        //scrProduct.setPreferredSize(new Dimension(350, 300));
        scrProduct.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable.getTableHeader());
    }

    /**
     *
     * @return the boolen value
     */
    private boolean valid() {
        if (!txfName.getText().equalsIgnoreCase("") && !txfPrice.getText().equalsIgnoreCase("")) {
            return true;
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scrProduct = new javax.swing.JScrollPane();
        tbProduct = new javax.swing.JTable();
        pnlNewPro = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        lblPrice = new javax.swing.JLabel();
        txfPrice = new javax.swing.JTextField();
        txfName = new javax.swing.JTextField();
        btnReset = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        lblInfo = new javax.swing.JLabel();
        scrInfo = new javax.swing.JScrollPane();
        txpInfo = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        pnlEdit = new javax.swing.JPanel();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        pnlProduct = new javax.swing.JPanel();
        lblProInfo = new javax.swing.JLabel();
        scrProInfo = new javax.swing.JScrollPane();
        txpProInfo = new javax.swing.JTextPane();
        jPanel1 = new javax.swing.JPanel();
        lblProduct = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(103, 27));
        setPreferredSize(new java.awt.Dimension(103, 27));
        setLayout(new java.awt.GridBagLayout());

        scrProduct.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "NEW PRODUCT", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N

        tbProduct.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tbProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbProduct.setIntercellSpacing(new java.awt.Dimension(5, 1));
        tbProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbProductMouseClicked(evt);
            }
        });
        scrProduct.setViewportView(tbProduct);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.8;
        add(scrProduct, gridBagConstraints);

        pnlNewPro.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "NEW PRODUCT", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlNewPro.setForeground(java.awt.Color.white);
        pnlNewPro.setMinimumSize(new java.awt.Dimension(200, 215));
        pnlNewPro.setPreferredSize(new java.awt.Dimension(200, 200));
        pnlNewPro.setLayout(new java.awt.GridBagLayout());

        lblName.setText("Name : (*)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 11, 0);
        pnlNewPro.add(lblName, gridBagConstraints);

        lblPrice.setText("Price($) : (*)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 4;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 11, 0);
        pnlNewPro.add(lblPrice, gridBagConstraints);

        txfPrice.setColumns(20);
        txfPrice.setPreferredSize(new java.awt.Dimension(300, 20));
        txfPrice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfPriceActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 170;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 11, 0);
        pnlNewPro.add(txfPrice, gridBagConstraints);

        txfName.setColumns(20);
        txfName.setMinimumSize(new java.awt.Dimension(15, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 161;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 11, 0);
        pnlNewPro.add(txfName, gridBagConstraints);

        btnReset.setBackground(new java.awt.Color(51, 102, 255));
        btnReset.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnReset.setForeground(new java.awt.Color(255, 255, 255));
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        pnlNewPro.add(btnReset, gridBagConstraints);

        btnAdd.setBackground(new java.awt.Color(51, 102, 255));
        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnAdd.setForeground(new java.awt.Color(255, 255, 255));
        btnAdd.setText("  Add  ");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 0, 0);
        pnlNewPro.add(btnAdd, gridBagConstraints);

        lblInfo.setText("Info : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 11, 0);
        pnlNewPro.add(lblInfo, gridBagConstraints);

        txpInfo.setMinimumSize(new java.awt.Dimension(50, 20));
        scrInfo.setViewportView(txpInfo);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 170;
        gridBagConstraints.ipady = 42;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 11, 14);
        pnlNewPro.add(scrInfo, gridBagConstraints);

        jLabel1.setText("(*) : Required");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 9, 0);
        pnlNewPro.add(jLabel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        add(pnlNewPro, gridBagConstraints);

        pnlEdit.setLayout(new java.awt.GridBagLayout());

        btnEdit.setBackground(new java.awt.Color(51, 102, 255));
        btnEdit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnEdit.setForeground(new java.awt.Color(255, 255, 255));
        btnEdit.setText("Edit Product");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        pnlEdit.add(btnEdit, gridBagConstraints);

        btnDelete.setBackground(new java.awt.Color(51, 102, 255));
        btnDelete.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnDelete.setForeground(new java.awt.Color(255, 255, 255));
        btnDelete.setText("Delete Product");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        pnlEdit.add(btnDelete, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.1;
        add(pnlEdit, gridBagConstraints);

        pnlProduct.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "NEW PRODUCT", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlProduct.setForeground(java.awt.Color.white);
        pnlProduct.setMinimumSize(new java.awt.Dimension(200, 94));
        pnlProduct.setPreferredSize(new java.awt.Dimension(200, 100));
        pnlProduct.setLayout(new java.awt.GridBagLayout());

        lblProInfo.setText("Product Info : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        pnlProduct.add(lblProInfo, gridBagConstraints);

        txpProInfo.setEditable(false);
        txpProInfo.setMinimumSize(new java.awt.Dimension(50, 20));
        scrProInfo.setViewportView(txpProInfo);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 170;
        gridBagConstraints.ipady = 42;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 11, 0, 14);
        pnlProduct.add(scrProInfo, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.3;
        add(pnlProduct, gridBagConstraints);

        jPanel1.setBackground(new java.awt.Color(51, 102, 255));
        jPanel1.setMinimumSize(new java.awt.Dimension(252, 30));
        jPanel1.setPreferredSize(new java.awt.Dimension(252, 30));
        jPanel1.setLayout(new java.awt.BorderLayout());

        lblProduct.setBackground(new java.awt.Color(51, 102, 255));
        lblProduct.setFont(new java.awt.Font("Tahoma", 1, 30));
        lblProduct.setForeground(new java.awt.Color(51, 102, 255));
        lblProduct.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/PRODUCTBANNER.jpg"))); // NOI18N
        lblProduct.setMaximumSize(new java.awt.Dimension(22222, 22222));
        lblProduct.setMinimumSize(new java.awt.Dimension(252, 50));
        lblProduct.setPreferredSize(new java.awt.Dimension(252, 37));
        jPanel1.add(lblProduct, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        add(jPanel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void txfPriceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfPriceActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txfPriceActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        //get index of the row is selected on table and arr
        int indexRow = tbProduct.getSelectedRow();
        if (indexRow != -1) {
            EditProductDlg product = new EditProductDlg(this, true);
            product.setSize(300, 350);
            //
            arr = proBuz.findByAllProduct();

            //set info on fields of EditProductDlg
            product.txfProID.setText("" + arr.get(indexRow).getProductID());
            product.txfName.setText(arr.get(indexRow).getProductName());
            product.txfPrice.setText("" + arr.get(indexRow).getPrice());
            product.txpInfo.setText(arr.get(indexRow).getInfo());
            //
            product.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "Please select Product");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        txfName.setText("");
        txfPrice.setText("");
        txpInfo.setText("");
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        Pattern pattern = Pattern.compile("[0-9]+[.]?[0-9]+");
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please input data in fields!");
        } else {
            Matcher m = pattern.matcher(txfPrice.getText());
            if (!m.matches()) {
                JOptionPane.showMessageDialog(this, "Please enter numeric value for Price!");
            } else {
                Product pro = new Product();
                pro.setProductName(txfName.getText());
                pro.setPrice(Float.valueOf(txfPrice.getText()));
                pro.setInfo(txpInfo.getText());
                pro.setStatus(true);
                if (!pro.validate()) {
                    JOptionPane.showMessageDialog(this, pro.getLastError());
                } else if (proBuz.createProduct(pro)) {
                    JOptionPane.showMessageDialog(this, proBuz.getLasterror());
                    loadTable();
                    btnResetActionPerformed(null);
                } else {
                    JOptionPane.showMessageDialog(this, proBuz.getLasterror());
                }
            }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        int indexRow = tbProduct.getSelectedRow();
        if (indexRow != -1) {
            arr = proBuz.findByAllProduct();
            int proID = arr.get(tbProduct.getSelectedRow()).getProductID();
            int opt = JOptionPane.showConfirmDialog(this, "Delete this Product?", "Delete Product", JOptionPane.YES_NO_OPTION);
            if (opt == 0) {
                if (proBuz.removeProduct(proID)) {
                    JOptionPane.showMessageDialog(this, proBuz.getLasterror());
                    loadTable();
                } else {
                    JOptionPane.showMessageDialog(this, proBuz.getLasterror());
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "please select Product!");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void tbProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProductMouseClicked

        arr = proBuz.findByAllProduct();
        txpProInfo.setText(arr.get(tbProduct.getSelectedRow()).getInfo());
    }//GEN-LAST:event_tbProductMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblProInfo;
    private javax.swing.JLabel lblProduct;
    private javax.swing.JPanel pnlEdit;
    private javax.swing.JPanel pnlNewPro;
    private javax.swing.JPanel pnlProduct;
    private javax.swing.JScrollPane scrInfo;
    private javax.swing.JScrollPane scrProInfo;
    private javax.swing.JScrollPane scrProduct;
    private javax.swing.JTable tbProduct;
    private javax.swing.JTextField txfName;
    private javax.swing.JTextField txfPrice;
    private javax.swing.JTextPane txpInfo;
    private javax.swing.JTextPane txpProInfo;
    // End of variables declaration//GEN-END:variables
}
